#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>	// Header File For The OpenGL32 Library
#include <OpenGL/glu.h>	// Header File For The GLu32 Library
#else
#include <GL/gl.h>	// Header File For The OpenGL32 Library
#include <GL/glu.h>	// Header File For The GLu32 Library
#endif

#include <iostream>
#include <fstream>
#include <string>

#include "Model.h"

using namespace std;

int name;
ifstream in;

float Model::convert(int* num) {
    if(num == NULL) {
        return 0.0f;
    }
    int i;
    int math = 10;
    for(i = 1; i < sizeof(num); i++) {
        math = math * 10;
    }
    int answer;
    for(i = 1; i < sizeof(num); i++) {
        math = math / 10;
        answer = answer + num[i] * math;
    }
    return (float)answer;
}

float Model::findVert() {
    float vert = 1.0f;
    return vert;
}

int Model::loadOBJ(char* filename) {
    name = glGenLists(1);
    glNewList(name, GL_COMPILE);
    cout << "Opening " << filename << endl;
    in.open(filename, ios::in);
    if(!in.is_open()) {
        cout << "Unable to open" << filename << endl;
        return 0;
    }
    cout << "Opened!" << endl;
    int count = 1;
    string line;
    in.seekg (0, ios::end);
    int length = in.tellg();
    in.seekg (0, ios::beg);
    while(in.tellg() < length) {
        getline (in, line);
        if(line[0] == 'v') {
            count++;
        }
    }
    in.seekg (0, ios::beg);
    float verts[count][3];
    int numVerts = -1;
    while(getline (in, line)) {
        if(line[0] == 'v') {
            cout << "Vert" << endl;
            numVerts++;
            verts[numVerts][0] = findVert();
            verts[numVerts][1] = findVert();
            verts[numVerts][2] = findVert();
        }
        else if(line[0] == 'f') {
            cout << "Face" << endl;
            /*glBegin(GL_TRIANGLES);
                glVertex3f(verts[(int)line[2]][0], verts[(int)line[2]][1], verts[(int)line[2]][2]);
                glVertex3f(verts[(int)line[4]][0], verts[(int)line[4]][1], verts[(int)line[4]][2]);
                glVertex3f(verts[(int)line[6]][0], verts[(int)line[6]][1], verts[(int)line[6]][2]);
            glEnd();*/

        }
    }
    glBegin(GL_TRIANGLES);						// Drawing Using Triangles
		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	glEnd();							// Finished Drawing The Triangle

    glEndList();
    in.close();
    cout << "Done" << endl;
    return 1;
}

int Model::draw() {
    glCallList(name);
}
